<?php

// +----------------------------------------------------------------------

// | Description: 客户

// +----------------------------------------------------------------------

// | Author: zl

// +----------------------------------------------------------------------



namespace app\crm\controller;



use app\admin\controller\ApiCommon;

use app\crm\model\BalanceLog;

use app\crm\model\Setting;

use app\crm\model\SettingCategory;

use app\crm\model\Service;

use app\crm\model\ReturnVist;

use app\crm\model\ReturnVistT;

use app\crm\model\Customer as CustomerModel;

use app\crm\model\TransferShop;

use app\crm\model\CustomerConfig as CustomerConfigModel;

use app\crm\model\CustomerGrade as CustomerGradeModel;

use think\response\Json;

use think\facade\Db;

use think\facade\Request;

use app\admin\model\Coupon as CouponModel;

use app\common\traits\ActionRecordTrait;


class AddQuestion extends ApiCommon

{
    protected $customerModel;

    protected $customerConfigModel;

    /**

     * 用于判断权限

     * @permission 无限制

     * @allow 登录用户可访问

     * @other 其他根据系统设置

     **/

    public function initialize()

    {

        $action = [

            'permission' => ['add_question'],

            'allow' => ['users', 'info']

        ];

        event('CheckAuth',$action);

        $request = $this->request;

        $a = strtolower($request->action());



        $this->customerModel = new CustomerModel();

        $this->customerConfigModel = new CustomerConfigModel();

        if (!in_array($a, $action['permission'])) {

            parent::initialize();

        }

    }

    /**
     * 新增调查问卷
     */
    public function add_question(){

        if (empty($_FILES['file'])) {

            echo '请选择上传文件<br><br><a href="http://burn.178590.com/return/addQuestion.html">返回【会员基本信息导入】页面</a>';exit;

        }

        $tmp_name = $_FILES['file']['tmp_name'];

        $question_status = $_POST['status'];

        $importData = readExcelData($tmp_name);

        // echo "<pre>";
        // print_r($importData);
        // echo "</pre>";
        // exit;

        $questionIds = [];

        foreach ($importData as $key => $value) {
            
            $question = $value[0]; // 题干
            $status = $value[2]; // 题目分类

            $where_question = [];

            array_push($where_question,['question_describe','=',$question]);

            $questionInfo = Db::name('question')->where($where_question)->select()->toArray();

            if($questionInfo){

                array_push($questionIds,$questionInfo[0]['id']);

            }else{

                if($status == 3){
                    // 简答题，无答案选项；

                    $question_addData = [
    
                        'question_describe' => $question,
         
                        'status'            => $status,

                        'create_time'       => time(),
        
                    ];

                    $questionID = Db::name('question')->insertGetId($question_addData);
    
                    array_push($questionIds,$questionID);

                }else{
    
                    $answers = explode('-',$value[1]); // 答案选项
    
                    $answerIds = [];
    
                    // 单选题
                    foreach ($answers as $k => $v) {
    
                        $where_answer = [];
    
                        array_push($where_answer,['describe','=',$v]);
    
                        $answer_list = Db::name('question_answer')->where($where_answer)->select()->toArray();
    
                        if($answer_list){
    
                            array_push($answerIds,$answer_list[0]['id']);
                            
                        }else{
    
                            $score = bcadd($k,1,0);
    
                            $addData = [
    
                                'describe' => $v,
    
                                'score'    => $score,
    
                            ];
    
                            $answerID = Db::name('question_answer')->insertGetId($addData);
    
                            array_push($answerIds,$answerID);
    
                        }
                        
                    }
    
                }
                
    
                // echo "<pre>";
                // print_r($answerIds);
                // echo "</pre>";
                // exit;

                $answer_list = implode(",",$answerIds);
    
                $question_addData = [
    
                    'question_describe' => $question,
    
                    'question_answer'   => $answer_list,
     
                    'status'            => $status,

                    'create_time'       => time(),
     
                ];
    
                // echo "<pre>";
                // print_r($question_addData);
                // echo "</pre>";
                // exit;
    
                $questionID = Db::name('question')->insertGetId($question_addData);
    
                array_push($questionIds,$questionID);

            }

        }

        $question_list = implode(",",$questionIds);

        $paper = [

            'qusetion_ids' => $question_list,

            'status'       => $question_status,

            'create_time'  => time(),

        ];


        $where_paper = [];

        array_push($where_paper,['qusetion_ids','=',$question_list]);

        array_push($where_paper,['status','=',$question_status]);

        $paperInfo = Db::name('questionnaire')->where($where_paper)->select()->toArray();

        // echo "<pre>";
        // print_r($paper);
        // echo "</pre>";
        // exit;
        
        if($paperInfo){

            echo '请勿重复添加调查卷！<br><br><a href="http://burn.178590.com/return/addQuestion.html">返回【问卷导入】页面</a>';exit;

        }else{

            $questionID = Db::name('questionnaire')->insertGetId($paper);

            echo '试卷添加成功！<br><br><a href="http://burn.178590.com/return/addQuestion.html">返回【问卷导入】页面</a>';exit;

        }
        
    }

}